package com.example.demo.controller.zabbix;

import com.example.demo.core.domain.AjaxResult;
import com.example.demo.zabbix.Zabbix;
import com.example.demo.zabbix.request.builder.request.AuthRequestBuilder;
import com.example.demo.zabbix.request.builder.request.UnAuthRequestBuilder;
import com.example.demo.zabbix.request.builder.alert.AlertGetRequestBuilder;
import com.example.demo.zabbix.request.builder.host.*;
import com.example.demo.zabbix.response.BaseResponse;
import org.jsoup.helper.StringUtil;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/zabbix/hostGroup")
public class HostGroupController {
    /**
     * @return 查询主机组列表信息
     */
    @GetMapping("/list")
    public AjaxResult list() {
        String auth = LoginController.cacheData.get("token");
        Zabbix zabbix = LoginController.zabbix;
        //      检索主机组
        BaseResponse<Boolean> hostResponse = zabbix.call(new HostGroupGetRequestBuilder(1L, auth).builder());
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.put("data", hostResponse.getResult());
        ajaxResult.put("code", 200);
        ajaxResult.put("msg", "查询成功");
        return ajaxResult;
    }

    /**
     * @return 新增主机组列表信息
     */
    @PostMapping
    public AjaxResult add(String hostGroupName) {
        String auth = LoginController.cacheData.get("token");
        Zabbix zabbix = LoginController.zabbix;
        BaseResponse<Map<String, List<String>>> hostGroupResponse = zabbix.call(new HostGroupCreateRequestBuilder(auth).setName(hostGroupName).builder());
        if (hostGroupResponse.getResult() == null) {
            return AjaxResult.error("新增主机组信息失败");
        } else {
            return AjaxResult.success();
        }
    }

    /**
     * 删除主机组列表信息
     */
    @PostMapping("/remove")
    public AjaxResult remove(List<String> ids) {
        String auth = LoginController.cacheData.get("token");
        Zabbix zabbix = LoginController.zabbix;
        BaseResponse<Map<String, List<String>>> hostGroupResponse = zabbix.call(new HostGroupDeleteRequestBuilder(auth).setIds(ids).builder());
        if (hostGroupResponse.getResult() == null) {
            return AjaxResult.error("删除主机组信息失败");
        } else {
            return AjaxResult.success();
        }
    }

    /**
     * 更新主机组列表信息
     */
    @PutMapping
    public AjaxResult edit(String groupId, String name) {
        String auth = LoginController.cacheData.get("token");
        Zabbix zabbix = LoginController.zabbix;
        Map<String, Object> map = new HashMap<>();
        if (!StringUtil.isBlank(groupId)) {
            map.put("groupid", groupId);
        }
        if (!StringUtil.isBlank(name)) {
            map.put("name", name);
        }
        BaseResponse<Map<String, List<String>>> hostGroupResponse = zabbix.call(new HostGroupUpdateRequestBuilder(auth).setParams(map).builder());
        if (hostGroupResponse.getResult() == null) {
            return AjaxResult.error("更新主机组信息失败");
        } else {
            return AjaxResult.success();
        }

    }

    public static void main(String[] args) {
        Zabbix zabbix = new Zabbix();
        zabbix.init("http://39.129.34.94:22800/");
        BaseResponse<Map> baseResponse = zabbix.call(new AuthRequestBuilder(1L).setUserName("Admin").setPassword("zabbix").builder());
        System.out.println("获取用户Token==>" + baseResponse.getResult());
//      登录
        BaseResponse<String> authResponse = zabbix.call(new AuthRequestBuilder(1L).setUserName("Admin").setPassword("zabbix").builder());
        String auth = authResponse.getResult();
//      检索警报
        BaseResponse<Map> alertResponse = zabbix.call(new AlertGetRequestBuilder(1L, auth).builder());
        System.out.println("检索警报==>" + alertResponse.getResult());
//      是否登录
        BaseResponse<Boolean> unAuthResponse = zabbix.call(new UnAuthRequestBuilder(1L, auth).builder());
        System.out.println("是否登录==>" + unAuthResponse.getResult());
//      检索主机
        BaseResponse<Boolean> hostResponse = zabbix.call(new HostGetRequestBuilder(1L, auth).builder());
        System.out.println("检索主机==>" + hostResponse.getResult());
//      检索主机组
        BaseResponse<Boolean> hostGroupResponse = zabbix.call(new HostGroupGetRequestBuilder(1L, auth).builder());
        System.out.println("检索主机组==>" + hostGroupResponse.getResult());
//        String auth2 = zabbix.auth("Admin", "zabbix");//login

    /*    BaseResponse<Map<String, List<String>>> userGroupResponse = zabbix.call(new UserGroupCreateRequestBuilder(zabbix.getAuth())//create user group
                .setName("testGroup")
                .builder());
        String userGroupId = userGroupResponse.getResult().get("usrgrpids").get(0);//get user group id
        zabbix.call(new UserCreateRequestBuilder(zabbix.getAuth())//create user and link to group by group id
                .setAlias("alias").setPasswd("p@ssw0rd")
                .setUsrgrps(new ArrayList() {{
                    add(new HashMap() {{
                        put("usrgrpid", userGroupId);
                    }});
                }}).builder());

        zabbix.unAuth(auth2);//zabbix.unAuth();//logout*/

    }
}
